<div id="page-body" class="agency-detail clearfix">
    <h2 style="margin-left: 10px;"><?php echo $franchise->name ?></h2>
    <table class="team_table">
        <?php foreach ($tearms as $key => $tearm) { ?>
            <tr>
                <td>
                    <img src="<?php echo base_url() . 'img/' . (!empty($tearm->avatar) ? $tearm->avatar : 'avatar/team_nopic.png'); ?>" alt="" class="team" />
                    <h2><?php echo $tearm->person_name ?></h2>
                    <div style="height:74px">
                        <p>
                            <strong><?php echo $tearm->job_title ?></strong><br /> 

                            <?php if (!empty($tearm->phone)) { ?>
                                <strong>Phone:</strong> <?php echo $tearm->phone ?><br /> 
                            <?php } ?>
                            <?php if (!empty($tearm->mobile)) { ?>
                                <strong>Mobile:</strong> <?php echo $tearm->mobile ?>
                            <?php } ?>
                        </p>
                    </div>

                    <div class="buttons">
                        <a id="emailagent<?php echo $key ?>-form-trigger" class="link-button agency-enquire" href="#">Email Agent</a>
                    </div>
                    <script type="text/javascript">
                        (function($) {
                            // Form dropdown button
                            $(function() {
                                var mcontent<?php echo $key ?> = $("#emailagent<?php echo $key ?>-form");
                                var mtrigger<?php echo $key ?> = $("#emailagent<?php echo $key ?>-form-trigger");
                                var mcancel<?php echo $key ?> = $("#emailagent<?php echo $key ?>-cancel");
                                        

                                           
                                mtrigger<?php echo $key ?>.click(function(e) {
                                    e.preventDefault();
                                    if (mcontent<?php echo $key ?>.is(":visible")) {
                                        mcontent<?php echo $key ?>.slideUp(500);
                                        mtrigger<?php echo $key ?>.removeClass('active');
                                    } else {
                                        mcontent<?php echo $key ?>.slideDown(500);
                                        mtrigger<?php echo $key ?>.addClass('active');
                                    }
                                });
                                mcancel<?php echo $key ?>.click(function(e) {
                                    e.preventDefault();
                                    if (mcontent<?php echo $key ?>.is(":visible")) {
                                        mcontent<?php echo $key ?>.slideUp(500);
                                        mtrigger<?php echo $key ?>.removeClass('active');
                                    }
                                });

                            });

                        })(jQuery);
                                    
                        $(document).ready(function(){
                            $('form#emailagent<?php echo $key ?>_mail').submit(function() {
                                var e = $(this);
                                $('#emailagent<?php echo $key ?>_mail .showError').html('');
                                $('#emailagent<?php echo $key ?>_mail .showSuccess').html('');
                                var form_data = $('#emailagent<?php echo $key ?>_mail').serialize();
                                //Begin the ajax call
                                $.ajax({
                                    url: "<?php echo site_url('offices/emailagent_email') ?>",
                                    type: "POST",
                                    data: form_data,
                                    dataType: "json",
                                    cache: false,
                                    success: function(json) {
                                        var usermessage = json.usermessage;
                                        var fielderrors = json.fielderrors;
                                        if (usermessage[0] == 'error')
                                        {
                                            //Show the user the errors.
                                            $('#emailagent<?php echo $key ?>_mail .showError').html(usermessage[1]);
                                            if(usermessage[2] == 1){
                                                $('#emailagent<?php echo $key ?>_mail .showError').html(usermessage[1]);
                                            }else{
                                                $(' #emailagent<?php echo $key ?>_mail .showError').show();
                                                $.each(fielderrors, function(id, value) {
                                                    $('#'+id).addClass('error');
                                                    $('#'+id).focus();
                                                });
                                            }
                                            
                                        } else {
                                            $('#emailagent<?php echo $key ?>_mail')[0].reset();
                                            $('#emailagent<?php echo $key ?>_mail .showError').html('');
                                            $('#emailagent<?php echo $key ?>_mail .showSuccess').html(usermessage[1]);
                                            $('#emailagent<?php echo $key ?>-form-trigger').focus();
                                        }
                                    }
                                });
                                return false;
                            });
                        });
                    </script>
                </td>

            </tr>
            <tr id="emailagent<?php echo $key ?>-form" style="display:none;" class="emailagent" enquire-target="">
                <td>
                    <div class="form-divider"></div>
                    <div id="emailoffice-form-content">
                        <div id="form-error-msg"></div>
                        <form id="emailagent<?php echo $key ?>_mail" class="cform appraisalrequest " method="post" capcharequest="" action="" enctype="multipart/form-data" name="agency-appraisal-form" onsubmit="">
                            <div class="showError"></div>
                            <div class="showSuccess"></div>
                            <ol class="cf-ol">
                                <li class="">
                                    <label for="name"><span>Your Name * </span></label>
                                    <input id="emailagent_id" name="emailagent_id" type="hidden" value="<?php echo $tearm->id ?>" />
                                    <input id="emailagent_name" name="emailagent_name" type="text" value=""/>
                                    <span class="reqtxt"></span></li>
                                <li class=""><label for="emailAddress"><span>Email * </span></label><input id="emailagent_email"  name="emailagent_email" type="text" value="" /><span class="emailreqtxt"></span></li>

                                <li class=""><label for="mobile"><span>Mobile </span></label>
                                    <input id="emailagent_mobile"  name="emailagent_mobile" type="text" value="" /></li>

                                <li class="" ><label for="message"><span>Message </span></label><textarea id="emailagent_message"  name="emailagent_message" style="width:280px; height:96px; resize: none"></textarea></li>
                            </ol>
                            <p class="cf-sb"><input type="submit" value="Submit" class="sendbutton" id="sendbutton3" name="sendbutton3"></p>		
                            <div class="clearer"></div>
                        </form>
                    </div>
                    <a id="emailagent<?php echo $key ?>-cancel" class="form-cancel" href="#">CANCEL</a>
                </td>
            </tr>
        <?php } ?>
    </table>
    <div class="contentright">
        <table>
            <tr>
                <td>
                    <h2><a href="javascript:changeStatus('sold')" class="status active" id="sold">For Sale</a></h2>
                </td>
            </tr>
            <tr>
                <td>
                    <h2><a href="javascript:changeStatus('rentals')" class="status" id="rentals">For Rent</a></h2>
                </td>
            </tr>
            <tr>
                <td>
                    <h2><a href="javascript:changeStatus('leased')" class="status" id="leased">Sold</a></h2> 
                </td>
            </tr>
            <tr>
                <td>
                    <div id="ofice_status" style="padding: 10px;" rel="0" status="sold"></div>
                </td>
            </tr>
            <tr>
                <td style="height:30px">
                    <a href="javascript:backoffice()"><img src="<?php echo base_url(); ?>img/arrow-top.png" alt="" style="margin-top: 10px" /></a>
                </td>
            </tr>
            <tr>
                <td style="height:30px">
                    <a href="javascript:nextoffice()"><img src="<?php echo base_url(); ?>img/arrow-bottom.png" alt="" style="margin-top: 10px" /></a>
                </td>
            </tr>
        </table>

    </div>
    <div class="clearer"></div>

    <script>
        
        
        function get_office(id,type,start,status){
            $.ajax({
                url: "<?php echo site_url('offices/get_office') ?>/"+type+'/'+start+'/<?php echo $franchise->id ?>',
                dataType: "HTML",
                cache: false,
                success: function(data) {
                    if(data){
                        $('#'+id).html(data);
                        $('#'+id).attr('rel',start);
                    }else if (start == 0){
                        $('#'+id).html('');
                    }
                        
                }
            });
        }
        
       
        function changeStatus(status){
            $('#ofice_status').attr('rel',0);
            $('#ofice_status').attr('status',status);
            $('.status').removeClass('active');
            $('#'+status).addClass('active')
            get_office('ofice_status',status,0);
        }
        
        function backoffice(){
            var start = $('#ofice_status').attr('rel');
            var type = $('#ofice_status').attr('status');
            start = parseInt(start);
            if(start > 0){
                start = start -1;
                get_office('ofice_status',type,start);
               
            }
        }
        
        function nextoffice(id,type){
            var start = $('#ofice_status').attr('rel');
            var type = $('#ofice_status').attr('status');
            start = parseInt(start)+1;
            get_office('ofice_status',type,start);
            
        }
        $(document).ready(function(){
            get_office('last_list','listings',0);
            get_office('ofice_status','sold',0);
        })
    </script>

</div>
